<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->lang->load('panel_' . $this->config->item('language'), $this->config->item('language'));
    }

    public function index() {
        
    }

    public function menu($indice = null) {
        $this->title = 'Menu' . $this->title;

        if (!empty($_POST)) {
            $this->data['error_bd'] = $this->menu->setMenu();
        }
        $this->load->model('usuario_model');
        $this->data['accion'] = $indice != null ? is_numeric($indice) ? 'editar' : 'crear'  : 'consultar';

        $this->css[] = PLUGINS . 'jquery-nestable/jquery.nestable.css';
        $this->css[] = PLUGINS . 'jquery-multi-select/css/multi-select.css';
        $this->javascript2[] = PLUGINS . 'jquery-multi-select/js/jquery.multi-select.js';
        $this->javascript2[] = SCRIPTSBACK . 'faunio-menu.js';
        $this->data['infomenu'] = $this->menu->getMenu();
        $this->data['roles'] = $this->usuario_model->getRoles();
        if ($indice != null) {
            $this->data['current'] = reset($this->menu->getMenu(array('men_id' => $indice)));
            $this->data['roles_menu'] = $this->menu->getRolesMenu($indice);
        }
        $this->data['f_formulario'] = $this->load->view('admin/menu/formulario', $this->data, true);
        $this->_renderback('admin/menu/menu');
    }

    public function usuarios($accion = null, $indice = null) {
        $this->title = 'Usuarios' . $this->title;
        $this->load->model('usuario_model');
        $this->data['accion'] = $indice != null ? is_numeric($indice) ? 'editar' : 'crear'  : 'consultar';
        
        //pulsamos editar
        if ($accion == 'editar') {
            $this->data['infouser'] = reset($this->usuario_model->getUsuario(array('usu_id' => $indice)));
            $this->data['usu_id'] = $indice;
            $this->data['roles'] = $this->usuario_model->getRoles();
            echo $this->load->view('admin/usuarios/formulario', $this->data, true);
            return;
        }
        
        //pulsamos borrar
        if ($accion == 'borrar') {
            $this->data['error_bd'] = $this->usuario_model->deleteUsuario($indice);
            redirect(PANEL.strtolower(__CLASS__)."/".__FUNCTION__);
        }

        //pulsamos guardar
        if (!empty($_POST)) {
            $this->data['error_bd'] = $this->usuario_model->setUsuario(array('origen' => 'admin'));
        }
        
        $this->css[] = PLUGINS . 'jquery-multi-select/css/multi-select.css';
        $this->javascript2[] = PLUGINS . 'jquery-multi-select/js/jquery.multi-select.js';
        $this->javascript2[] = SCRIPTSBACK . 'faunio-usuarios.js';
        $this->data['infousers'] = $this->usuario_model->getUsuario();
        $this->_renderback('admin/usuarios/usuarios');
    }

}